
import tiktoken


def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613"):
    """Return the number of tokens used by a list of messages."""
    try:
        encoding = tiktoken.encoding_for_model(model)
    except KeyError:
        print("Warning: model not found. Using cl100k_base encoding.")
        encoding = tiktoken.get_encoding("cl100k_base")

    num_tokens = len(encoding.encode(messages))
    return num_tokens

str = "This is the first string."

response = num_tokens_from_messages(str)
print(response)




# 初始化 GPT-3 编码器
encoding = tiktoken.get_encoding("cl100k_base")

# 示例字符串数组
strings = [
    "This is the first string.",
    "Here is the second one.",
    "And this is the third string."
]

# 计算每个字符串的 token 数量
token_counts = [len(encoding.encode(string)) for string in strings]

# 输出结果
for i, count in enumerate(token_counts):
    print(f"String {i + 1} token count: {count}")

